package com.hbu.ware.hbumallware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbu.common.constant.WareConstant;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.Query;
import com.hbu.ware.hbumallware.dao.PurchaseDao;
import com.hbu.ware.hbumallware.entity.PurchaseDetailEntity;
import com.hbu.ware.hbumallware.entity.PurchaseEntity;
import com.hbu.ware.hbumallware.service.PurchaseDetailService;
import com.hbu.ware.hbumallware.service.PurchaseService;
import com.hbu.ware.hbumallware.service.WareSkuService;
import com.hbu.ware.hbumallware.vo.PurchaseDoneVO;
import com.hbu.ware.hbumallware.vo.PurchaseItemDoneVO;
import com.hbu.ware.hbumallware.vo.PurchaseMergeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 90458
 */
@Service("purchaseService")
@AllArgsConstructor
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    private PurchaseDetailService purchaseDetailService;

    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreceiveList(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", 0).or().eq("status", 1)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mergePurchase(PurchaseMergeVO purchaseMergeVO) {
        // purchaseId == null ? 新建采购单 : 合并采购单到purchasedId
        Long purchaseId = purchaseMergeVO.getPurchaseId();
        if (purchaseId == null || purchaseId == 0) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = purchaseMergeVO.getItems();
        ArrayList<PurchaseDetailEntity> purchaseDetailList = new ArrayList<>(items.size());
        for (Long item : items) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(item);
            detailEntity.setPurchaseId(purchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            purchaseDetailList.add(detailEntity);
        }
        purchaseDetailService.updateBatchById(purchaseDetailList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getList(List<Long> ids) {
        // 1、确定当前采购单是新建或者已分配状态
        List<PurchaseEntity> collect = ids.stream().map(this::getById)
                .filter(item -> item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode()
                        || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode())
                .peek(item -> item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode())).collect(Collectors.toList());
        this.updateBatchById(collect);
        purchaseDetailService.updateStatusByPurchaseId(ids);
    }

    @Override
    public void done(PurchaseDoneVO purchaseDone) {
        boolean finishFlag = Boolean.TRUE;
        List<PurchaseItemDoneVO> purchaseItemDoneList = purchaseDone.getItems();
        for (PurchaseItemDoneVO purchaseItem : purchaseItemDoneList) {
            Integer status = purchaseItem.getStatus();
            Assert.notNull(status, "采购状态不应为空");
            // 两种情况 status == 3 采购完成  status == 4 采购失败
            finishFlag = finishFlag & WareConstant.PurchaseDetailStatusEnum.FINISH.getCode() == status;
            if (WareConstant.PurchaseDetailStatusEnum.FINISH.getCode() == status) {
                // 采购成功要进行入库
                PurchaseDetailEntity purchaseDetail = purchaseDetailService.getById(purchaseItem.getItemId());
                wareSkuService.putInStorage(purchaseDetail);
            }
            purchaseDetailService.updateStatusById(purchaseItem.getItemId(), status);
        }
        baseMapper.updateStatusById(purchaseDone.getId(), finishFlag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.ERROR.getCode());

    }


}

